package com.dongdongshop.seller.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.operator.pojo.TbSeller;
import com.dongdongshop.operator.service.SellerService;
import com.dongdongshop_util.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("register")
public class RegisterController {

    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Reference(check = false)
    private SellerService sellerService;

    //跳转到商家注册页面
    @RequestMapping("toRegister")
    public String toRegister(){
        return "register";
    }

    //添加注册操作
    @ResponseBody
    @RequestMapping("addRegister")
    public Map<String,String> addRegister(TbSeller seller){
        Map<String,String> map = new HashMap<>();
        try {
            //判断账号密码是否为空
            if(seller.getSellerId() ==null || "".equals(seller.getSellerId()) || seller.getPassword() ==null || "".equals(seller.getPassword())){
                map.put("code","账号和密码不能为空");
                return map;
            }
            //根据用户名查询该用户是否存在
            TbSeller seller1=sellerService.selectById(seller.getSellerId());
            if(seller1 !=null){
                map.put("code","该账号已存在");
                return map;
            }
            seller.setStatus("0");
            seller.setSalt("90aa5a");
            String password = ShiroUtils.encryptPassword("MD5", seller.getPassword(), "90aa5a", 10);
            seller.setPassword(password);
            seller.setCreateTime(new Date());
            sellerService.addRegister(seller);
            map.put("code","注册成功，正在审核");
            return map;
        }catch (Exception e){
            logger.error("出现错误",e);
            map.put("code","出现错误");
            return map;
        }
    }


}
